package com.xiaohudie.Ioliu;

/**
 * @Author xiao hu die
 * @Date 2022/1/8 18:22
 * @Version 1.0
 */

import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**处理对象的流(也是处理流)*/
public class Test05 {
    public static void main(String[] args) {

    }
    /**序列化保存文件*/
  @Test
    public void test01() throws Exception{
        String filePath ="D:\\data.dat";
      ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(filePath));
       objectOutputStream.writeInt(100);
        objectOutputStream.writeUTF("刘蝶");
        objectOutputStream.writeBoolean(true);
        XuLieHua xuLieHua =new XuLieHua("刘蝶",100);
        objectOutputStream.writeObject(xuLieHua);
        objectOutputStream.close();
      System.out.println("序列化保存结束");
  }
  /**反序列化输出文件*/
  @SneakyThrows
  @Test
    public void test02(){
      String filePath ="D:\\data.dat";
      ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(filePath));
      int i = objectInputStream.readInt();
      System.out.println(i+1);
      String s = objectInputStream.readUTF();
      System.out.println(s);
      boolean b = objectInputStream.readBoolean();
      System.out.println(b);
      Object ld = objectInputStream.readObject();
      /**向下转型*/
      XuLieHua xuLieHua =(XuLieHua) ld;
      System.out.println(xuLieHua.getName());
      System.out.println(ld.getClass());
      System.out.println(ld);
      objectInputStream.close();
  }
}
